/* eslint-disable */
import * as Request from '@/api/liubike';
import {config} from "@/api/config"
import store from "@/tools/factory"
let that = null;
let speed = 100;
let t1 = null;
let processTime = null;
let polling = null;
let pollingCount = 0;
import { Toast, Dialog } from "mand-mobile";

export default {
    components: {
        [Dialog.name]: Dialog,
        [Toast.name]: Toast
    },
    data() {
        return {
            currentProgress: 0,
            progressNum: 0
        }
    },
    mounted() {
        that = this;
        t1 = store.get("t1");
        this.currentProgress = 0;
        this.progressNum = 0;
        if (!store.get("loginInfo")) {
            that.close();
            return false
        }
        that.proccessConfig();
        that.unlockPolling();
    },
    methods: {
        orderHandle() {
            Request.getUserOrder().then(res => {
                if (!res.success) { // (360锁)未查询到订单,即未开锁,轮询多次
                    return false
                }
                // 开锁成功后相关操作
                store.set('orderno', res.data.orderNo);
                that.unlockSuccess();
            }).catch(err => {
                that.unlockFail()
            });
        },
        unlockSuccess() {
            that.clearTime();
            this.progressNum = 0;
            this.currentProgress = '100%'
            store.set('firstFlag', "noFlag");
            store.set("bannerStatus", "open");
            that.$router.back(-1)
        },
        outTimeHandle() {
            clearInterval(polling);
            polling = null;
            wx.showToast({
                icon: 'none',
                title: '开锁超时,请重试或更换车辆',
                duration: 2000,
                success() {
                    setTimeout(function () {
                        wx.navigateBack({
                            delta: 1
                        });
                    }, 2000)
                }
            });
        },
        unlockPolling() {
            clearInterval(polling);
            pollingCount = 0;
            let progressConfig = store.get("progressConfig");
            let time = 0;
            if (progressConfig) {
                time = progressConfig[0].second + progressConfig[1].second + progressConfig[2].second;
            } else {
                time = 35
            }
            let maxCount = parseInt(time / 2) + 1;
            polling = setInterval(() => {
                if (!store.get("loginInfo")) {
                    that.unlockCancel()
                }
                if (pollingCount > maxCount) {
                    that.outTimeHandle()
                    return false;
                }
                that.orderHandle();
                pollingCount += 1; //每刷新一次 pollingCount 就+1
            }, 2000);
        },
        proccessConfig() {
            let proccessConfig = store.get("progressConfig");
            processTime = setInterval(function () {
                if (pollingCount * 2 <= proccessConfig[0].second) {
                    that.progressNum = that.progressNum + (proccessConfig[0].progress / proccessConfig[0].second) / 10;
                } else if (pollingCount * 2 > proccessConfig[0].second && pollingCount * 2 < proccessConfig[0].second + proccessConfig[1].second) {
                    that.progressNum = that.progressNum + (proccessConfig[1].progress / proccessConfig[1].second) / 10;
                } else {
                    that.progressNum = that.progressNum + (proccessConfig[2].progress / (proccessConfig[2].second - 1)) / 10;
                }
                // 当进度条为100时清除定时任务
                if (that.progressNum >= 99) {
                    clearInterval(processTime);
                }
                that.currentProgress = parseInt(that.progressNum) + `%`
                // 并且把当前的进度值设置到progress中
            }, speed)
        },
        clearTime() {
            clearInterval(polling);
            clearInterval(processTime);
            polling = null;
            processTime = null;
        },
        close() {
            Request.unlockCancel().then(res => {
                that.unlockCancel()
            }).catch(err => {
                that.unlockCancel()
            })
        },
        unlockCancel() {
            that.clearTime();
            that.$router.back(-1)
        },
        unlockFail() {
            clearInterval(polling);
            pollingCount = 0;
            polling = null;
            Dialog.alert({
                title: '温馨提示',
                content: '车辆开锁失败',
                confirmText: '确定',
                onConfirm: function () {
                    that.$router.back(-1)
                }
            })
        },
    }
}
